﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using BYUStock.Comman;
using ScottPlot;
using ScottPlot.Plottable;

namespace BYUStock.View
{
    public partial class CandleChart : Form
    {
        public Action<int> ViewMouseWheel { get; set; }

        CandleCollection Candles { get; set; } = null;
        IOHLC[] OHLCs { get; set; } = null;
        FinancePlot CurrCandlePlot { get; set; } = null;

        public CandleChart(CandleCollection candles)
        {
            this.Candles = candles;
            InitializeComponent();
        }

        private void CandleChart_Load(object sender, EventArgs e)
        {
            InitPlot();
            SetCandles(Candles);
        }

        void InitPlot()
        {
            plot.Plot.Style(Style.Black);
            plot.MouseWheel += ((o, e) =>
            {
                ViewMouseWheel?.Invoke(e.Delta);
            });
        }

        public void SetCandles(CandleCollection candles)
        {
            if (candles == null)
            {
                Candles = null;
                OHLCs = null;
                plot.Plot.Clear();
                plot.Refresh();
                return;
            }
            Candles = candles;
            OHLCs = CandleParser.CandleCollectionToIOHLCArray(Candles);
            RefreshPlot();
        }

        void RefreshPlot()
        {
            plot.Plot.Clear();
            CurrCandlePlot = plot.Plot.AddCandlesticks(OHLCs);
            CurrCandlePlot.Sequential = true;
            CurrCandlePlot.ColorUp = Color.Red;
            CurrCandlePlot.ColorDown = Color.Green;
            var xFormat = GetCandleChartXLabalFormat(Candles.Span);
            plot.Plot.XAxis.TickLabelFormat(xFormat);
            plot.Plot.AxisAutoX();
            plot.Plot.AxisAutoY();
            plot.Refresh();
        }
        Func<double, string> GetCandleChartXLabalFormat(CandleSpan span)
        {
            return (double index) =>
            {
                if (index < 0 || index > OHLCs.Length - 1) return string.Empty;
                switch (span)
                {
                    case CandleSpan.Minute1:
                        return OHLCs[(int)index].DateTime.ToShortTimeString();
                    case CandleSpan.Minute5:
                        return OHLCs[(int)index].DateTime.ToShortTimeString();
                    case CandleSpan.Minute15:
                        return OHLCs[(int)index].DateTime.ToShortTimeString();
                    case CandleSpan.Minute30:
                        return OHLCs[(int)index].DateTime.ToShortTimeString();
                    case CandleSpan.Minute60:
                        return OHLCs[(int)index].DateTime.ToShortTimeString();
                    case CandleSpan.Minute120:
                        return OHLCs[(int)index].DateTime.ToShortTimeString();
                    case CandleSpan.Day:
                        return OHLCs[(int)index].DateTime.ToShortDateString();
                    case CandleSpan.Week:
                        return OHLCs[(int)index].DateTime.ToShortDateString();
                    case CandleSpan.Month:
                        return OHLCs[(int)index].DateTime.ToShortDateString();
                    case CandleSpan.Year:
                        return OHLCs[(int)index].DateTime.ToShortDateString();
                    default:
                        throw new NotImplementedException();
                }
            };
        }
    }
}
